package cn.mj.redis;

import static org.junit.Assert.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.junit.Before;
import org.junit.Test;

import cn.mj.domain.ToDoTask;
import redis.clients.jedis.Jedis;

public class RedisOper {
	
	private Jedis jedis ;
	
	@Before
	public void before() {
		jedis = new Jedis("192.168.0.201", 6379);
	}
	
	
	@Test
	public void test1() throws Exception {
		Map<String, Double> scoreMembers = new HashMap<String, Double>();
		for (int i = 0; i < 20; i++) {
			scoreMembers.put("yemali_" + i, i *100.1);
		}
		jedis.zadd("yemali", scoreMembers);
	}
	
	@Test
	public void test2() throws Exception {
		Set<String> set = jedis.zrevrange("yemali", 0, 4);
//		System.out.println(set);
		for (String key : set) {
			List<String> list = jedis.hmget(key, "id","loginName","userName","url","title");
			System.out.println(new ToDoTask(list.get(0), list.get(1), list.get(2), list.get(3), list.get(4)));
		}
	}
	
	
	@Test
	public void test3() throws Exception {
		for (int i = 0; i < 20; i++) {
			Map<String, String> hash = new HashMap<String, String>();
			ToDoTask toDoTask = new ToDoTask("" + i*100, "yemali", "叶玛丽", "http://10.185.101.3/ylhdc?instid=" + i*100, "考勤申请" + i*100);
//			BeanUtils.copyProperties(hash, toDoTask);
			hash = BeanUtils.describe(toDoTask);
//			System.out.println(hash);
			jedis.hmset("yemali_" + i, hash );
		}
	}
}
